package com.wxyzz.cem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.wxyzz.cem.enums.ThreadLocalName;
import com.wxyzz.cem.exception.UserServiceException;
import com.wxyzz.cem.interceptor.BackAdminInterceptor;
import com.wxyzz.cem.interceptor.UserInterceptor;
import com.wxyzz.cem.pojo.ClassInfo;
import com.wxyzz.cem.pojo.User;
import com.wxyzz.cem.pojo.enums.UserRole;
import com.wxyzz.cem.service.ClassInfoService;
import com.wxyzz.cem.service.UserService;
import com.wxyzz.cem.util.Result;
import com.wxyzz.cem.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 班级 前端控制器
 * </p>
 *
 * @author xc
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/class-info")
public class ClassInfoController {
    @Autowired
    private ClassInfoService classInfoService;
    @Autowired
    private UserService userService;

    @PostMapping
    @UserInterceptor.UserIntercept
    public Result addClassInfo(@RequestBody ClassInfo classInfo) {
        boolean b = classInfoService.addClassInfo(classInfo);
        return b ? Result.success() : Result.error();
    }


    @GetMapping("/list")
    public Result<PageDTO<ClassInfo>> getClassList(@RequestParam(defaultValue = "1" ,required = false) Integer page,
                                                @RequestParam(defaultValue = "1" ,required = false) Integer pageSize,
                                                @RequestParam(required = false) String name,
                                                @RequestParam(required = false) List<Integer> ids){
        PageDTO<ClassInfo> classList = classInfoService.getClassList(page, pageSize, name, ids);
        return Result.success(classList);
    }
    
    /**
     * 获取当前班级信息
     */
    @UserInterceptor.UserIntercept
    @GetMapping("/current")
    public Result<ClassInfo> getCurrentClass(){
        User user = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        if (user.getClassId() == null) {
            throw new UserServiceException("未加入班级");
        }
        return Result.success(classInfoService.getById(user.getClassId()));
    }


    @GetMapping("/identify")
    @UserInterceptor.UserIntercept
    public Result identify(){
        User user = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        UserRole role = user.getRole();
        return role.equals(UserRole.CLASS_ADMIN) ? Result.success() : Result.error();
    }


    @PutMapping
    @UserInterceptor.UserIntercept
    public Result updateClassInfo(@RequestBody ClassInfo classInfo) {
        User user = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        UserRole role = user.getRole();
        if (role.equals(UserRole.USER)){
            throw new UserServiceException("该用户无操作权限！");
        }else {
            boolean b = classInfoService.updateClassInfo(classInfo);
            return b ? Result.success() : Result.error();
        }
    }
    
    /**
     * 加入班级
     */
    @PutMapping("/join/{classId}")
    @UserInterceptor.UserIntercept
    public Result<Object> joinClassById(@PathVariable Integer classId){
        User user = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        classInfoService.joinClassById(user, classId);
        return Result.success();
    }
    
    /**
     * 退出班级
     */
    @PutMapping("/exit")
    @UserInterceptor.UserIntercept
    public Result<Object> exitClass(){
        User user = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        classInfoService.exitClass(user);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @UserInterceptor.UserIntercept
    public Result deleteClassInfoById(@PathVariable Integer id) {
        User user = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        UserRole role = user.getRole();
        if (role.equals(UserRole.USER)){
            throw new UserServiceException("该用户无操作权限！");
        }else {
            return classInfoService.deleteClass(id) ? Result.success() : Result.error();
        }
    }
    
    /**
     * 获取当前班级的班级成员
     */
    @GetMapping("/member")
    @UserInterceptor.UserIntercept
    public Result<Page<User>> getClassMember(@RequestParam(defaultValue = "1" ,required = false) Integer page,
                                 @RequestParam(defaultValue = "10",required = false) Integer size,
                                 @RequestParam(required = false) String name,
                                 @RequestParam(required = false) String account,
                                 @RequestParam(required = false) String phone
                                      ) {
        User user = ThreadLocalUtil.get(ThreadLocalName.USER.getValue(), User.class);
        if (user.getClassId() == null) {
            throw new UserServiceException("未加入班级");
        }
        Page<User> userPage = userService.getClassMemberById(user.getClassId(), page, size, name, account, phone);
        return Result.success(userPage);
    }


    @PutMapping("ban/{classId}")
    @BackAdminInterceptor.BackAdminIntercept
    public Result banClassById(@PathVariable Integer classId) {
        boolean b = classInfoService.banClassById(classId);
        return b ? Result.success() : Result.error();
    }

    @PutMapping("unban/{id}")
    @BackAdminInterceptor.BackAdminIntercept
    public Result unbanClassById(@PathVariable Integer id) {
        boolean b = classInfoService.unbanClassById(id);
        return b ? Result.success() : Result.error();
    }


    @GetMapping("/{id}")
    @UserInterceptor.UserIntercept
    public Result getClassInfoById(@PathVariable Integer id) {
        ClassInfo classInfo = classInfoService.getById(id);
        return classInfo != null ? Result.success(classInfo) : Result.error();
    }
}
